package com.bankcomm.ui;

import com.bankcomm.R;
import com.bankcomm.util.LPUtils;
import com.rytong.tools.ui.WaitProgressDialog;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class BOCWebViewActivity extends Activity implements OnClickListener {
	WebView myWebView;
	private WebSettings mySettings;
	private ImageButton btnReture;//返回
	private ImageButton btnBack;//返回
	private ImageButton btnForward;//向前
	private ImageButton btnRefresh;//刷新

	private Button btnTitleBack;// TITLE上的返回
	private WaitProgressDialog dialog;
	private TextView textTitle;
	private RelativeLayout topLayout;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String url = getIntent().getStringExtra("url");

		hideSystemBar(this);
		setContentView(R.layout.layout_web_main);

		topLayout =(RelativeLayout)findViewById(R.id.linearLayout1);
		textTitle =(TextView)findViewById(R.id.txtTitle); 
		
		LPUtils.scaledTHeightRel(topLayout);
		myWebView = (WebView) findViewById(R.id.webView);
		myWebView.loadUrl(url);
		myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		myWebView.requestFocus();
		setConfig();

		btnReture=(ImageButton)findViewById(R.id.return_btn);
		btnBack = (ImageButton) findViewById(R.id.back_btn);
		btnForward=(ImageButton)findViewById(R.id.forward_btn);
        btnRefresh=(ImageButton) findViewById(R.id.refresh_btn);

		btnTitleBack = (Button) findViewById(R.id.backtitle);
        LPUtils.setTitleButtonWH(btnTitleBack);
		btnReture.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		btnForward.setOnClickListener(this);
		btnRefresh.setOnClickListener(this);
		btnTitleBack.setOnClickListener(this);

		//设置名字标题
		String name = getIntent().getStringExtra("title");
		if(null != name && !"".equalsIgnoreCase(name)){
			textTitle.setText(name);
		}
		myWebView.setWebViewClient(client);
		dialog = new WaitProgressDialog(this,getString(R.string.onMapLoading));
		dialog.show(null);
	}
	
	// 待用
	WebViewClient client = new WebViewClient() {
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			if(null != url && url.startsWith("tel:")){
				//拨打电话
				Intent telIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
				startActivity(telIntent);
			}else if(null != url &&  url.startsWith("sms:")){
				//发送短信
	        	Intent intent = new Intent(android.content.Intent.ACTION_SENDTO,Uri.parse(url));
	        	startActivity(intent);
			}else{
				view.loadUrl(url);				
			}
			return true;
		}

		public void onPageFinished(WebView view, String url) {
			dialog.dismiss();
		}

		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			dialog.dismiss();
		}
		
		public void onReceivedSslError(WebView view, android.webkit.SslErrorHandler handler, android.net.http.SslError error) {
			handler.proceed();
		};
	};
	
	

	protected void setConfig() {
		mySettings = myWebView.getSettings();
		mySettings.setSupportZoom(true);
		mySettings.setJavaScriptEnabled(true);
		mySettings.setBuiltInZoomControls(true);
		mySettings.setLoadsImagesAutomatically(true);
	}

	protected void hideSystemBar(Activity activity) {
		// No Statusbar隐藏界面最上方状态栏
		Window win = activity.getWindow();
		// 隐藏android系统定义的标题栏
		activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			if (keyCode == KeyEvent.KEYCODE_BACK) {
				handlerBackPress();
				return true;
			}
		}
		return false;
	}

	public void handlerBackPress() {
		if (myWebView.canGoBack()) {
			myWebView.goBack();
		} else {
			myWebView.stopLoading();
			this.finish();
		}
	}

	@Override
	public void onClick(View v) {
		Bundle inBundle = new Bundle();
		switch (v.getId()) {
		case R.id.return_btn:
			handlerBackPress();
			break;
		case R.id.back_btn:
			if (myWebView.canGoBack()) {
				myWebView.goBack();
			}
			break;
		case R.id.forward_btn:
			if (myWebView.canGoForward()) {
				myWebView.goForward();
			}
			break;
		case R.id.refresh_btn:
			myWebView.reload();
			break;
		case R.id.backtitle:
			this.finish();
			break;
	  }
	}
}